using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Srvtools;

namespace cPpoPo1
{
    public partial class Form1 : InfoForm
    {
        public Form1()
        {
            InitializeComponent();
        }
        ////////////
        public string GetToday()
        {
            return string.Format("{0:yyyyMMdd}", DateTime.Today);
        }

        public string GetNowTime()
        {
            return string.Format("{0:hhmmss}", DateTime.Now);
        }

        public string GetUser()
        {
            return CliUtils.fLoginUser;
        }

        private void ibsMaster_BeforeEdit(object sender, EventArgs e)
        {                                 
            //dateTime顯示
            infoTextBox5.Text = DateTime.Now.ToLongTimeString();
                   
        }

        private void ibsMaster_CurrentChanged(object sender, EventArgs e)
        {
            string state = infoNavigator1.GetCurrentState();
            if (state == "Inserting" || state == "Editing")
            {
                //修改時間顯示
                string UPDATE_TIME = ((DataRowView)ibsMaster.Current)["UPDATE_TIME"].ToString();
                if (UPDATE_TIME.Length == 6)
                 infoTextBox5.Text = Convert.ToDateTime(UPDATE_TIME.Substring(0, 2) + ":" + UPDATE_TIME.Substring(2, 2) + ":" + UPDATE_TIME.Substring(4, 2)).ToLongTimeString(); 
                else if (UPDATE_TIME.Length == 0)
                    infoTextBox5.Text = "";


                //建檔時間顯示
                string CREATE_TIME = ((DataRowView)ibsMaster.Current)["CREATE_TIME"].ToString();
                if (CREATE_TIME.Length == 6)
                 infoTextBox3.Text = Convert.ToDateTime(CREATE_TIME.Substring(0, 2) + ":" + CREATE_TIME.Substring(2, 2) + ":" + CREATE_TIME.Substring(4, 2)).ToLongTimeString(); 
                else if (CREATE_TIME.Length == 0)
                    infoTextBox3.Text = "";
            }
        }

        private void InfoRefValBox01_Leave(object sender, EventArgs e)
        {                           
            //在選定Refval之後帶入多筆明細  
            string stat = infoNavigator1.GetCurrentState();
            if (stat == "Inserting" || stat == "Editing")
            {
                ((DataRowView)ibsMaster.Current).EndEdit(); //使明細帶入的時候不會出現Key值錯誤                   
                dsPR1_D.SetWhere("ID_VEND = '" + InfoRefValBox01.TextBoxSelectedValue + "'" + " and "+ "ID_WH='"+InfoRefValBox03.TextBoxSelectedValue+"'"+" and CD_PO=1");//雙條件,廠商編號+倉庫代號
                if (InfoRefValBox01.TextBoxSelectedValue == "" || InfoRefValBox03.TextBoxSelectedValue == "")
                { }
                else
                {
                    //清除明細內容
                    int n = ibsDetail.List.Count;
                    for (int i = 0; i < n; i++)
                    {
                        ibsDetail.List.RemoveAt(0);
                    }
                    //將所要的明細插入  
                    DataRow dr = null;
                    double QT_QTY = 0, QT_PO_END = 0, PR_SALES = 0, AM_SALES = 0;

                    for (int i = 0; i < dsPR1_D.RealDataSet.Tables[0].Rows.Count; i++)
                    {
                        QT_QTY = Convert.ToDouble(dsPR1_D.RealDataSet.Tables[0].Rows[i]["QT_QTY"].ToString());
                        QT_PO_END = Convert.ToDouble(dsPR1_D.RealDataSet.Tables[0].Rows[i]["QT_PO_END"].ToString());

                        //每筆重算總金額
                        int SEQ = 0;
                        PR_SALES = Convert.ToDouble(dsPR1_D.RealDataSet.Tables[0].Rows[i]["PR_SALES"].ToString());
                        AM_SALES = (QT_QTY - QT_PO_END) * PR_SALES;
                        if (QT_QTY - QT_PO_END <= 0)//如果數量為0,則不帶出明細
                        { }
                        else
                        {
                            SEQ++;
                            dr = Master.RealDataSet.Tables[1].NewRow();
                            dr["QT_QTY"] = Convert.ToString(QT_QTY - QT_PO_END);
                            dr["QT_QTY_TEMP"] = Convert.ToString(QT_QTY - QT_PO_END);
                            dr["NO_PO"] = InfoTextBox02.Text;
                            dr["ID_VEND"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["ID_VEND"].ToString();
                            dr["CN_SEQ2"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["CN_SEQ"].ToString();
                            dr["CN_SEQ"] = SEQ.ToString("00");
                            dr["CD_IMUM"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["CD_IMUM"].ToString();
                            dr["PR_SALES"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["PR_SALES"].ToString();
                            dr["NO_ITEM"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["NO_ITEM"].ToString();
                            dr["NM_ITEM"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["NM_ITEM"].ToString();
                            dr["NO_PR"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["NO_PR"].ToString();
                            dr["CD_PO"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["CD_PO"].ToString();  //有在Server端的infoCommand做Left-join    
                            dr["NO_ORDER"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["NO_ORDER"].ToString();//有在Server端的infoCommand做Left-join
                            dr["NO_INQ"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["NO_INQ"].ToString();
                            dr["FG_COUNT"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["FG_COUNT"].ToString();
                            dr["QT_PUR_END"] = "0";
                            dr["AM_SALES"] = AM_SALES.ToString();
                            Master.RealDataSet.Tables[1].Rows.Add(dr);
                        }

                    }
                }
                if (ibsDetail.List.Count == 0 && InfoRefValBox03.TextBoxText != "")
               { System.Windows.Forms.MessageBox.Show("此廠商的相關明細已採購完畢!"); }

                //計算金額,採購金額未知               
                     double RI_TAX = 0, AM_EXCHG = 0, AM_CURRENCY = 0, AM_TOTAL = 0, RI_EXCHG = 0, AM_TAX = 0;
                              
                    //RI_TAX為稅率
                    if (InfoComboBox03.Text == "免稅")
                        RI_TAX = 0;
                    else if (InfoComboBox03.Text == "應稅")
                        RI_TAX = 0.05;

                    if (InfoTextBox03.Text == "")
                        AM_CURRENCY = 0;
                    else
                    {
                        AM_CURRENCY = Convert.ToDouble(infoDataGridView1.DoSum(8,totalMode.sum));//採購金額
                        RI_EXCHG = Convert.ToDouble(InfoTextBox06.Text);//匯率
                        AM_EXCHG = AM_CURRENCY * RI_EXCHG;//換算金額
                        AM_TAX = AM_EXCHG * RI_TAX;//稅額
                        AM_TOTAL = AM_EXCHG + AM_TAX;//總金額(含稅)
                        //把值丟入TEXTBOX
                        InfoTextBox03.Text = AM_CURRENCY.ToString();//採購金額
                        InfoTextBox08.Text = AM_EXCHG.ToString();//換算金額
                        InfoTextBox12.Text = AM_TAX.ToString();//稅額
                        InfoTextBox21.Text = AM_TOTAL.ToString();//總金額(含稅)
                    }
                
               
            }
            else if ((stat != "Inserting") && (stat != "Editing"))
            { return; }
        }

        private void infoDataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
             
            if ((infoNavigator1.GetCurrentState() == "Inserting") || (infoNavigator1.GetCurrentState() == "Editing"))
            {
                float AM_AMT = 0, QT_QTY = 0, QT_QTY_TEMP = 0;
                string Qty = infoDataGridView1.Columns[e.ColumnIndex].DataPropertyName.ToString();
                if (Qty == "QT_QTY")  
                {
                    //QT_QTY不能大於QT_QTY_TEMP
                    QT_QTY = float.Parse(((DataRowView)ibsDetail.Current)["QT_QTY"].ToString());
                    QT_QTY_TEMP = float.Parse(((DataRowView)ibsDetail.Current)["QT_QTY_TEMP"].ToString());
                    if (QT_QTY > QT_QTY_TEMP)
                    {
                        System.Windows.Forms.MessageBox.Show("採購數量超過請購數量");
                        ((DataRowView)ibsDetail.Current)["QT_QTY"] = ((DataRowView)ibsDetail.Current)["QT_QTY_TEMP"].ToString();
                    }
                    //計算實售金額＝數量*售價 
                    AM_AMT = float.Parse(((DataRowView)ibsDetail.Current)["PR_SALES"].ToString());
                    AM_AMT = AM_AMT * float.Parse(((DataRowView)ibsDetail.Current)["QT_QTY"].ToString());
                    ((DataRowView)ibsDetail.Current)["AM_SALES"] = AM_AMT.ToString();
                    ibsDetail.EndEdit();

                    //重新計算金額              
                    double RI_TAX = 0, AM_EXCHG = 0, AM_CURRENCY = 0, AM_TOTAL = 0, RI_EXCHG = 0, AM_TAX = 0;

                    //RI_TAX為稅率
                    if (InfoComboBox03.Text == "免稅")
                        RI_TAX = 0;
                    else if (InfoComboBox03.Text == "應稅")
                        RI_TAX = 0.05;

                    if (InfoTextBox03.Text == "")
                        AM_CURRENCY = 0;
                    else
                    {
                        AM_CURRENCY = Convert.ToDouble(infoDataGridView1.DoSum(8, totalMode.sum));//採購金額
                        RI_EXCHG = Convert.ToDouble(InfoTextBox06.Text);//匯率
                        AM_EXCHG = AM_CURRENCY * RI_EXCHG;//換算金額
                        AM_TAX = AM_EXCHG * RI_TAX;//稅額
                        AM_TOTAL = AM_EXCHG + AM_TAX;//總金額(含稅)
                        //把值丟入TEXTBOX
                        InfoTextBox03.Text = AM_CURRENCY.ToString();//採購金額
                        InfoTextBox08.Text = AM_EXCHG.ToString();//換算金額
                        InfoTextBox12.Text = AM_TAX.ToString();//稅額
                        InfoTextBox21.Text = AM_TOTAL.ToString();//總金額(含稅)
                    }                    
                }

               
                
            }
        }

        private void InfoComboBox03_TextChanged(object sender, EventArgs e)
        {
            //計算金額,採購金額已知,稅別變換時
            string stat = infoNavigator1.GetCurrentState();
            double RI_TAX = 0, AM_EXCHG = 0, AM_CURRENCY = 0, AM_TOTAL = 0, RI_EXCHG = 0, AM_TAX = 0;
            if ((stat == "Inserting") || (stat == "Editing"))
            {
                //RI_TAX為稅率
                if (InfoComboBox03.Text == "免稅")
                    RI_TAX = 0;
                else if (InfoComboBox03.Text == "應稅")
                    RI_TAX = 0.05;

                if (InfoTextBox03.Text == "")
                    AM_CURRENCY = 0;
                else
                {
                    AM_CURRENCY = Convert.ToDouble(infoDataGridView1.DoSum(8, totalMode.sum));//採購金額
                    RI_EXCHG = Convert.ToDouble(InfoTextBox06.Text);//匯率
                    AM_EXCHG = AM_CURRENCY * RI_EXCHG;//換算金額
                    AM_TAX = AM_EXCHG * RI_TAX;//稅額
                    AM_TOTAL = AM_EXCHG + AM_TAX;//總金額(含稅)
                    //把值丟入TEXTBOX
                    InfoTextBox03.Text = AM_CURRENCY.ToString();//採購金額
                    InfoTextBox08.Text = AM_EXCHG.ToString();//換算金額
                    InfoTextBox12.Text = AM_TAX.ToString();//稅額
                    InfoTextBox21.Text = AM_TOTAL.ToString();//總金額(含稅)
                }
            }
            else if ((stat != "Inserting") && (stat != "Editing"))
            { return; }
        }

        private void ibsMaster_AddingNew(object sender, AddingNewEventArgs e)
        {
            //在新增就顯示建檔時間    
            infoTextBox3.Text = DateTime.Now.ToLongTimeString();
        }

        public bool CheckRow(string Value)
        {
            if (ibsDetail.List.Count == 0)
                return false;
            else
                return true;
        }

        private void bindingNavigatorEditItem_Click(object sender, EventArgs e)
        {
            //以string儲存,在按下修改時
            ((DataRowView)ibsMaster.Current)["UPDATE_USER"] = CliUtils.fLoginUser;
            ((DataRowView)ibsMaster.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsMaster.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now);

        }

        private void InfoRefValBox03_Leave(object sender, EventArgs e)
        {
             //在選定Refval之後帶入多筆明細  
            string stat = infoNavigator1.GetCurrentState();
            if (stat == "Inserting" || stat == "Editing")
            {
                ((DataRowView)ibsMaster.Current).EndEdit(); //使明細帶入的時候不會出現Key值錯誤                   
                dsPR1_D.SetWhere("ID_VEND = '" + InfoRefValBox01.TextBoxSelectedValue + "'"+"  and  "+"ID_WH='"+InfoRefValBox03.TextBoxSelectedValue+"'"+" and "+"CD_PO = 1");//雙條件,廠商編號+倉庫代號
                if (InfoRefValBox01.TextBoxSelectedValue == "" || InfoRefValBox03.TextBoxSelectedValue == "")
                { }
                else
                {
                    //清除明細內容
                    int n = ibsDetail.List.Count;
                    for (int i = 0; i < n; i++)
                    {
                        ibsDetail.List.RemoveAt(0);
                    }
                    //將所要的明細插入  
                    DataRow dr = null;
                    double QT_QTY = 0, QT_PO_END = 0, PR_SALES = 0, AM_SALES = 0;
                    int SEQ = 0;
                    for (int i = 0; i < dsPR1_D.RealDataSet.Tables[0].Rows.Count; i++)
                    {
                        QT_QTY = Convert.ToDouble(dsPR1_D.RealDataSet.Tables[0].Rows[i]["QT_QTY"].ToString());
                        if (dsPR1_D.RealDataSet.Tables[0].Rows[i]["QT_PO_END"] == null || dsPR1_D.RealDataSet.Tables[0].Rows[i]["QT_PO_END"].ToString() == "")
                            QT_PO_END = 0;
                        else
                        QT_PO_END = Convert.ToDouble(dsPR1_D.RealDataSet.Tables[0].Rows[i]["QT_PO_END"].ToString());

                        //每筆重算總金額
                        PR_SALES = Convert.ToDouble(dsPR1_D.RealDataSet.Tables[0].Rows[i]["PR_SALES"].ToString());
                        AM_SALES = (QT_QTY - QT_PO_END) * PR_SALES;
                        if (QT_QTY - QT_PO_END <= 0)//如果數量為0,則不帶出明細
                        { }
                        else
                        {
                            SEQ++;//項次計數

                            dr = Master.RealDataSet.Tables[1].NewRow();
                            dr["QT_QTY"] = Convert.ToString(QT_QTY - QT_PO_END);
                            dr["QT_QTY_TEMP"] = Convert.ToString(QT_QTY - QT_PO_END);
                            dr["NO_PO"] = InfoTextBox02.Text;
                            dr["CN_SEQ"] = SEQ.ToString("00");
                            dr["ID_VEND"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["ID_VEND"].ToString();
                            dr["CN_SEQ2"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["CN_SEQ"].ToString();
                            dr["CD_IMUM"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["CD_IMUM"].ToString();
                            dr["PR_SALES"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["PR_SALES"].ToString();
                            dr["NO_ITEM"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["NO_ITEM"].ToString();
                            dr["NM_ITEM"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["NM_ITEM"].ToString();
                            dr["NO_PR"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["NO_PR"].ToString();
                            dr["CD_PO"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["CD_PO"].ToString();
                            dr["NO_ORDER"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["NO_ORDER"].ToString();
                            dr["NO_INQ"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["NO_INQ"].ToString();
                            dr["FG_COUNT"] = dsPR1_D.RealDataSet.Tables[0].Rows[i]["FG_COUNT"].ToString();
                            dr["QT_PUR_END"] = "0";
                            dr["AM_SALES"] = AM_SALES.ToString();
                            Master.RealDataSet.Tables[1].Rows.Add(dr);
                        }

                    }
                }
                if (ibsDetail.List.Count == 0 && InfoRefValBox01.TextBoxText != "")
                { System.Windows.Forms.MessageBox.Show("此廠商的相關明細已採購完畢!"); }

                //計算金額,採購金額未知               
                     double RI_TAX = 0, AM_EXCHG = 0, AM_CURRENCY = 0, AM_TOTAL = 0, RI_EXCHG = 0, AM_TAX = 0;
                              
                    //RI_TAX為稅率
                    if (InfoComboBox03.Text == "免稅")
                        RI_TAX = 0;
                    else if (InfoComboBox03.Text == "應稅")
                        RI_TAX = 0.05;

                    if (InfoTextBox03.Text == "")
                        AM_CURRENCY = 0;
                    else
                    {
                        AM_CURRENCY = Convert.ToDouble(infoDataGridView1.DoSum(8,totalMode.sum));//採購金額
                        //RI_EXCHG = Convert.ToDouble(InfoTextBox06.Text);//匯率
                        AM_EXCHG = AM_CURRENCY * RI_EXCHG;//換算金額
                        AM_TAX = AM_EXCHG * RI_TAX;//稅額
                        AM_TOTAL = AM_EXCHG + AM_TAX;//總金額(含稅)
                        //把值丟入TEXTBOX
                        InfoTextBox03.Text = AM_CURRENCY.ToString();//採購金額
                        InfoTextBox08.Text = AM_EXCHG.ToString();//換算金額
                        InfoTextBox12.Text = AM_TAX.ToString();//稅額
                        InfoTextBox21.Text = AM_TOTAL.ToString();//總金額(含稅)
                    }
                
               
            }
            else if ((stat != "Inserting") && (stat != "Editing"))
            { return; }
            }

        private void ibsMaster_AfterEdit(object sender, EventArgs e)
        {
            //以string儲存
            ((DataRowView)ibsMaster.Current)["UPDATE_USER"] = CliUtils.fLoginUser;
            ((DataRowView)ibsMaster.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsMaster.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now);

        }

        private void infoDataGridView2_Click(object sender, EventArgs e)
        {
            //重新設定明細欄位的ReadOnly屬性(因為使用AutoDisableControl會造成回歸預設值False)  
            this.infoDataGridView2.ReadOnly = true;
        }

        private void infoDataGridView1_Click(object sender, EventArgs e)
        {
            //重新設定明細欄位的ReadOnly屬性(因為使用AutoDisableControl會造成回歸預設值False)  
            this.NO_PO.ReadOnly = true;
            this.CN_SEQ2.ReadOnly = true;
            this.cNSEQDataGridViewTextBoxColumn.ReadOnly = true;
            this.nMITEMDataGridViewTextBoxColumn.ReadOnly = true;
            this.cDIMUMDataGridViewTextBoxColumn.ReadOnly = true;
            this.pRSALESDataGridViewTextBoxColumn.ReadOnly = true;
            this.aMSALESDataGridViewTextBoxColumn.ReadOnly = true;
            this.nOORDERDataGridViewTextBoxColumn.ReadOnly = true;
            this.qTPURENDDataGridViewTextBoxColumn.ReadOnly = true;
            this.FG_COUNT.ReadOnly = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //隱藏鍵值欄位
            this.NO_PO.Visible = false;            
        }

        private void ibsDetail_BeforeEdit(object sender, EventArgs e)
        {
            //記錄Detail的修改時間日期
            ((DataRowView)ibsDetail.Current)["UPDATE_USER"] = CliUtils.fLoginUser;
            ((DataRowView)ibsDetail.Current)["UPDATE_DATE"] = string.Format("{0:yyyyMMdd}", DateTime.Now.Date);
            ((DataRowView)ibsDetail.Current)["UPDATE_TIME"] = string.Format("{0:hhmmss}", DateTime.Now);
        }

        private void bindingNavigatorPrintItem_Click(object sender, EventArgs e)
        {
            rPpoPo11.Form1 Print = new rPpoPo11.Form1();
            Print.Wstr = "PPO_PO1_M.NO_PO='" + InfoTextBox02.Text + "'";
            Print.MdiParent = this.MdiParent;
            Print.Show();
        }

        private void infoNavigator1_StateChanged(object sender, InfoNavigatorStateChangedEventArgs e)
        {
            //將DateTimePicker設為不可改
            this.InfoDateTimePicker01.Enabled = false;
            this.InfoDateTimePicker1.Enabled = false;
        }

        private void infoDataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            //重新計算金額
            string stat = infoNavigator1.GetCurrentState();
            double RI_TAX = 0, AM_EXCHG = 0, AM_CURRENCY = 0, AM_TOTAL = 0, RI_EXCHG = 0, AM_TAX = 0;
            if ((stat == "Inserting") || (stat == "Editing"))
            {               
                //RI_TAX為稅率
                if (InfoComboBox03.Text == "免稅")
                    RI_TAX = 0;
                else if (InfoComboBox03.Text == "應稅")
                    RI_TAX = 0.05;

                if (InfoTextBox03.Text == "")
                    AM_CURRENCY = 0;
                else
                {
                    AM_CURRENCY = Convert.ToDouble(infoDataGridView1.DoSum(8, totalMode.sum));//採購金額
                    RI_EXCHG = Convert.ToDouble(InfoTextBox06.Text);//匯率
                    AM_EXCHG = AM_CURRENCY * RI_EXCHG;//換算金額
                    AM_TAX = AM_EXCHG * RI_TAX;//稅額
                    AM_TOTAL = AM_EXCHG + AM_TAX;//總金額(含稅)
                    //把值丟入TEXTBOX
                    InfoTextBox03.Text = AM_CURRENCY.ToString();//採購金額
                    InfoTextBox08.Text = AM_EXCHG.ToString();//換算金額
                    InfoTextBox12.Text = AM_TAX.ToString();//稅額
                    InfoTextBox21.Text = AM_TOTAL.ToString();//總金額(含稅)
                }                
            }
            else if ((stat != "Inserting") && (stat != "Editing"))
            { return; }
        }

        private void bindingNavigatorAddNewItem_MouseUp(object sender, MouseEventArgs e)
        {
            //將DateTimePicker設為不可改
            this.InfoDateTimePicker01.Enabled = false;
            this.InfoDateTimePicker1.Enabled = false;
        }
                        
       
    }

        
}

